package com.vms.control.weight.ui;

import java.util.Date;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

import com.vms.control.weight.R;
import com.vms.control.weight.model.DateModel;
import com.vms.control.weight.model.DateModel.IOnDateChanged;
import com.vms.control.weight.model.WeightModel;
import com.vms.control.weight.ui.AnimationHolder.AnimVariant;

public class DayChangeUi implements View.OnClickListener, IOnDateChanged{

	protected AnimationHolder mAnimHolder;
	protected Activity mActivity;
	protected WeightModel mWeightModel;
	
	private TextSwitcher mDayText;
	protected TextSwitcher mMonthText;
	protected TextSwitcher mYearText;
	
	protected ImageButton mPrevDay;
	protected ImageButton mNextDay;
	
	
	public DayChangeUi(Activity activity, WeightModel weightModel) {
		super();
		this.mActivity = activity;
		this.mWeightModel = weightModel;
		this.mAnimHolder = new AnimationHolder(mActivity.getApplicationContext());
		findView();
		loadAnimations();
		weightModel.registerDateChangeListener(this);
		weightModel.updateDate();
	}
	
	private void findView() {
		mPrevDay = (ImageButton) mActivity.findViewById(R.id.prev_day_button);
		mNextDay = (ImageButton) mActivity.findViewById(R.id.next_day_button);
		mDayText = (TextSwitcher) mActivity.findViewById(R.id.day_text);
		mMonthText = (TextSwitcher) mActivity.findViewById(R.id.month_text);
		mYearText = (TextSwitcher) mActivity.findViewById(R.id.year_text);
		mPrevDay.setOnClickListener(this);
		mNextDay.setOnClickListener(this);
		mDayText.setFactory(mFactory);
		mMonthText.setFactory(mFactory);
		mYearText.setFactory(mFactory);
	}
	
	protected void loadAnimations () {
		mAnimHolder.put(AnimVariant.FIRST, mDayText , R.anim.mslide_in_left, R.anim.mslide_out_right);
		mAnimHolder.put(AnimVariant.SECOND, mDayText , R.anim.mslide_in_right, R.anim.mslide_out_left);
		
		mAnimHolder.put(AnimVariant.FIRST, mMonthText , R.anim.mslide_in_left, R.anim.mslide_out_right);
		mAnimHolder.put(AnimVariant.SECOND, mMonthText , R.anim.mslide_in_right, R.anim.mslide_out_left);
		
		mAnimHolder.put(AnimVariant.FIRST, mYearText , R.anim.mslide_in_left, R.anim.mslide_out_right);
		mAnimHolder.put(AnimVariant.SECOND, mYearText , R.anim.mslide_in_right, R.anim.mslide_out_left);
	}
	
	private void updateAnimation (int direction) {
		AnimVariant variant = (direction == R.id.prev_day_button)? AnimVariant.FIRST : AnimVariant.SECOND;
    	mAnimHolder.apply(variant, mDayText);
    	mAnimHolder.apply(variant, mMonthText);
    	mAnimHolder.apply(variant, mYearText);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.prev_day_button:
			updateAnimation(v.getId());
			mWeightModel.setPrevDay().updateDate();
			break;
		case R.id.next_day_button:
			updateAnimation(v.getId());
			mWeightModel.setNextDay().updateDate();
			break;
		}
	}

	@Override
	public void onDateChanged(Date date, boolean isToday) {
		updateTextSwitcher(mDayText,DateModel.textDay(date,isToday));
		updateTextSwitcher(mMonthText,DateModel.textMonth(date,isToday));
		updateTextSwitcher(mYearText,DateModel.textYear(date));
	}
	
	private void updateTextSwitcher( TextSwitcher ts, CharSequence text ) {
		if(((TextView)ts.getCurrentView()).getText().equals(text))
			return;
		ts.setText(text);
	}
	
	
    private ViewFactory mFactory = new ViewFactory() {

        @Override
        public View makeView() {
            TextView t = new TextView(mActivity);
            t.setGravity(Gravity.CENTER_HORIZONTAL);
            t.setTextAppearance(mActivity, R.style.LargeTExtMarginLR);
            return t;
        }
    };
}
